// my/pages/xtxNoShip/xtxNoShip.js
var http = require('../../../utils/https');
Page({
  data: {
    orderId: '',
    allOrderIemDetails: [],
    currentOrderData: {},
    orderInfo: {
      deliveryFee: '0.00'
    },
    showItems: true
  },
  onLoad(options) {
    if (options && options.orderId) {
      this.setData({
        orderId: options.orderId
      });
      this.loadOrder(options.orderId)
    }
    if (this.data.orderId) {
      this.loadOrderItems(this.data.orderId);
    }
  },
  loadOrder(orderId) {
    http.GET(`/order/orders/${orderId}`, {}, (res) => {
      if (res.code == 200) {
        this.setData({
          currentOrderData: res.data
        })
      }
      console.log('加载订单成功:', res);

    }, (err) => {
      console.log('加载订单成功:', res);
    })
  },
  loadOrderItems(orderId) {
    http.GET('/order/item/list', {
      orderId
    }, (res) => {
      const formatted = (res.rows || []).map(it => ({
        ...it,
        subtotal: Number(it.subtotal).toFixed(2),
        unitPrice: Number(it.unitPrice).toFixed(2)
      }));
      this.setData({
        allOrderIemDetails: formatted
      });
    }, () => {});
  },
  toggleShowItems() {
    this.setData({
      showItems: !this.data.showItems
    });
  },
  navigateBack() {
    wx.navigateBack({
      delta: 1,
      fail: () => {
        wx.switchTab({
          url: '/pages/my/my'
        });
      }
    });
  },
  applyAfterSale() {
    wx.showToast({
      title: '申请售后提交成功',
      icon: 'success'
    });
  },
  orderAgain() {
    wx.showToast({
      title: '已加入购物车',
      icon: 'success'
    });
  }
});